package com.resourcestore.action;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;

import com.resourcestore.utils.CommonUtil;
import com.resourcestore.action.BaseAction;
import com.resourcestore.dao.MajorDao;
import com.resourcestore.dao.MajorGroupDao;
import com.resourcestore.model.Major;
import com.resourcestore.model.MajorGroup;
import com.resourcestore.utils.tag.Pagination;

public class MajorAction extends BaseAction {

	private Major major;
	private List<Major> listMajor;
	private MajorDao majorDao = new MajorDao();
	private MajorGroup majorGroup;
	//private List<MajorGroup> preInsert;
	private MajorGroupDao majorGroupDao = new MajorGroupDao();
	private Pagination pagination = new Pagination();

	/**
	 * 查询返回List
	 * 
	 * @throws ParseException
	 * 
	 */
	public String listMajor() {

		major = new Major();
		pagination.setTotal(majorDao.queryMajorNum(major));
		listMajor = majorDao.selectMajorPage(major, pagination);

		for (int i = 0; i < listMajor.size(); i++) {
			try {
				listMajor.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listMajor.get(i)
								.getGmtCreated()));
				listMajor.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listMajor.get(i)
								.getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "default";

	}

	/**
	 * 根据条件搜索，查询返回List带分页
	 * 
	 * @throws ParseException
	 * 
	 */
	public String searchListMajor() {

		// 对内容进行urldecoder解码
		try {
			major.setMajorgroupName(new String(major.getMajorgroupName()
					.getBytes("iso-8859-1"), "UTF-8"));
			major.setDepartmentName(new String(major.getDepartmentName()
					.getBytes("iso-8859-1"), "UTF-8"));
			major.setMajorName(new String(major.getMajorName().getBytes(
					"iso-8859-1"), "UTF-8"));
			major.setAuthor(new String(
					major.getAuthor().getBytes("iso-8859-1"), "UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		pagination.setTotal(majorDao.queryMajorNum(major));
		listMajor = majorDao.selectMajorPage(major, pagination);

		// 将bigint数字处理为时间格式
		for (int i = 0; i < listMajor.size(); i++) {
			try {
				listMajor.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listMajor.get(i)
								.getGmtCreated()));
				listMajor.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listMajor.get(i)
								.getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return newpath("/Major/listMajor.jsp");
	}

	/**
	 * 预处理插入信息
	 */
	public String preInsertMajor() {
//		preInsert = majorGroupDao.selectMajorGroup();
//
//		for (int i = 0; i < preInsert.size(); i++) {
//			try {
//				preInsert.get(i).setGmtCreatedString(
//						CommonUtil.getSysTimeFormatString(preInsert.get(i)
//								.getGmtCreated()));
//				preInsert.get(i).setGmtModifiedString(
//						CommonUtil.getSysTimeFormatString(preInsert.get(i)
//								.getGmtModified()));
//			} catch (ParseException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
		return newpath("/Major/insertMajor.jsp");
	}

	/**
	 * 插入一条数据
	 */
	public String insertMajor() {

		try {
			major.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			major.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		majorDao.insertMajor(major);

		return redirect("/Major/listMajor.action");

	}

	/**
	 * 更新之前加载原信息
	 * 
	 */
	public String preUpdateMajor() {

		major = majorDao.selectMajorById(major);

		//preInsert = majorGroupDao.selectMajorGroup();

		return newpath("/Major/updateMajor.jsp");
	}

	/**
	 * 更新信息
	 * 
	 */
	public String updateMajor() {

		try {
			// major.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			major.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		majorDao.updateMajor(major);
		return redirect("/Major/listMajor.action");
	}

	/**
	 * 删除信息
	 */
	public String deleteMajor() {
		majorDao.deleteMajor(major);
		return redirect("/Major/listMajor.action");
	}

	/**
	 * 批量删除信息
	 */
	public String deleteMoreMajor() {
	
		majorDao.deleteMoreMajor(major);
		return redirect(this.getUrl());
	}

	public MajorGroup getMajorGroup() {
		return majorGroup;
	}

	public void setMajorGroup(MajorGroup majorGroup) {
		this.majorGroup = majorGroup;
	}

//	public List<MajorGroup> getPreInsert() {
//		return preInsert;
//	}
//
//	public void setPreInsert(List<MajorGroup> preInsert) {
//		this.preInsert = preInsert;
//	}

	public Major getMajor() {
		return major;
	}

	public void setMajor(Major major) {
		this.major = major;
	}

	public List<Major> getListMajor() {
		return listMajor;
	}

	public void setListMajor(List<Major> listMajor) {
		this.listMajor = listMajor;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

}
